﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataManageInfoDao : DaoService<MetaDataManageInfo>
    {
        public MetaDataManageInfoDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_ManageInfo";
            }
        }
        public List<string> ExceptColumnNameList { get; set; } = new List<string>();
        public override List<string> ExceptColumnName
        {
            get
            {
                return ExceptColumnNameList;
            }
        }

        public override Dictionary<string, object> GetDataMapping(MetaDataManageInfo data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("Importance");
            datas.Add("Importance", (int)(data.Importance));

            return datas;
        }

        public override MetaDataManageInfo ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);

            myCol.Remove("Importance");

            var data = base.ConvertToObject(myCol, row);

            if (row["Importance"] != DBNull.Value)
            {
                data.Importance = (MetaDataImportance)Enum.Parse(typeof(MetaDataImportance),
                                                Convert.ToString(row["Importance"]));
            }
            else
            {
                data.Importance = MetaDataImportance.UnKnow;
            }


            return data;
        }

        public void DeleteByMetaDataId(string metadataID, string projectBranchID = "")
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("MetaDataID=@MetaDataID");
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataID));
            if (string.IsNullOrEmpty(projectBranchID) == false && projectBranchID != "Main")
            {
                sqlCondition = string.Format("{0} and ProjectBranchId=@ProjectBranchId", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchId", TDbDataType.VarChar, projectBranchID));
            }
            var sql = string.Format("delete from {0} where {1}", TableName, sqlCondition);
            DB.ExecSqlStatement(sql, paramsList.ToArray());
        }

        public MetaDataManageInfo GetMetaDataManageInfo(string metadataID, string projectBranchID="")
        {
            MetaDataManageInfo manageInfo = new MetaDataManageInfo();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("MetaDataID=@MetaDataID");
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metadataID));
            string columns = "ID,MetadataID,MetaDataCode, MetaDataTypeID, KeyAppId, KeyAppName, ModuleID, ModuleName, Importance, Tag, IsDeleted, Creator, CreateTime, LastModifier, LastModifyTime, FuncID, FuncName, '' as ProjectBranchID";
            if (string.IsNullOrEmpty(projectBranchID) == false && projectBranchID != "Main")
            {
                sqlCondition = string.Format("{0} and ProjectBranchId=@ProjectBranchId", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@ProjectBranchId", TDbDataType.VarChar, projectBranchID));
                columns = "ID,MetadataID,MetaDataCode, MetaDataTypeID, KeyAppId, KeyAppName, ModuleID, ModuleName, Importance, Tag, IsDeleted, Creator, CreateTime, LastModifier, LastModifyTime, FuncID, FuncName, ProjectBranchID";
            }
            var ds = this.DB.ExecuteDataSet(string.Format("select {0} from {1} where {2}", columns, TableName, sqlCondition), paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    manageInfo = ConvertToObject(base.Columns, row);                   
                }
            }
            return manageInfo;
        }
    }
}
